package com.ykw.parking.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@Component
public class MailServiceImpl implements MailService{
    @Autowired
    private JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String from;
    @Override//简单文本邮件
    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);//邮件发送者
        message.setSubject(subject);//主题
        message.setTo(to);//邮件接受者
        message.setText(content);//邮件内容
        mailSender.send(message);
    }
    @Override//富文本邮件
    //发送富文本邮件需要使用MimeMessageHelper类
    // MimeMessageHelper支持发送复杂邮件模板，支持文本、附件、HTML、图片等。
    public void sendInlineResourceMail(String to,String num) throws MessagingException {
        String subject="栓马桩注册信息";
        String rscPath="C:/Users/凯文的computer/IdeaProjects/parking/src/main/resources/static/Imperial/assets/img/sc.php";
        String rscId="001";
        String content="<html><body><div align='center'><img src=\'cid:"+rscId+"\'><h2>您的验证码为:"+num+"(3分钟内有效)</h2></div></body></html>";
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        File file = new File(rscPath);
        FileSystemResource res = new FileSystemResource(file);
        helper.addInline(rscId, res);
        mailSender.send(message);
    }

    @Override
    public void sendInlineResourceText(String to, String num) throws MessagingException {
        String subject="栓马桩重置信息";
        String rscPath="C:/Users/凯文的computer/IdeaProjects/parking/src/main/resources/static/Imperial/assets/img/sc.php";
        String rscId="001";
        String content="<html><body><div align='center'><img src=\'cid:"+rscId+"\'><h2>您的临时密码为:"+num+" 请登录到我的页面修改自己的密码！</h2></div></body></html>";
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        File file = new File(rscPath);
        FileSystemResource res = new FileSystemResource(file);
        helper.addInline(rscId, res);
        mailSender.send(message);
    }
}
